package org.lmj.dos.web.controller;

import org.lmj.dos.common.bean.ServerResponse;
import org.lmj.dos.common.utils.qrcode.QrcodeGenerator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@RestController
@RequestMapping
public class PayController {
    @RequestMapping("/qrcode")
    public void qrcode(HttpServletResponse response){
        BufferedImage ok = QrcodeGenerator.encode("OK", 200, 200);
        if(ok!=null){
            try {
                ImageIO.write(ok, "JPEG", response.getOutputStream());
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("打印异常:com.controller.Business_Ctrl.getImg(String, HttpServletResponse)");
            }
        }
        return ;
    }

    @RequestMapping(value = "/pay",produces="text/html;charset=utf-8")
    public String aliPay(String money){
        String payHtml="<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>支付页面</title>\n" +
                "    <script type=\"text/javascript\">\n" +
                "        function countDown(secs, surl) {\n" +
                "            var jumpTo = document.getElementById('jumpTo');\n" +
                "            jumpTo.innerHTML = secs;\n" +
                "            if (--secs > 0) {\n" +
                "                setTimeout(\"countDown(\" + secs + \",'\" + surl + \"')\", 1000);\n" +
                "            } else {\n" +
                "                location.href = surl;\n" +
                "            }\n" +
                "        }\n" +
                "    </script>\n" +
                "</head>\n" +
                "<body>\n" +
                "<br>\n" +
                "<br>\n" +
                "<br>\n" +
                "<br>\n" +
                "<center>\n" +
                "<span >请使用支付宝扫码支付</span>\n" +
                "<br>\n" +
                "<br>\n" +
                "    <span >扫一扫，付款(元)</span><br><br>\n" +
                "    <span style=\"color: red\">"+money+"</span><br>\n" +
                "<img src=\"http://localhost:9999/api/qrcode\">\n" +
                "<br>\n" +
                "<br>\n" +
                "<span id=\"jumpTo\">5</span>秒后自动跳转到商户页面\n" +
                "</center>\n" +
                "<script type=\"text/javascript\">countDown(5, 'http://localhost:8000/#/order/list');</script>\n" +
                "</body>\n" +
                "</html>";

        //含有中文的数据序列化后在网络中传输一定要注意进行base64加密解密，否则页面显示数据会出现乱码
        //produces="text/html;charset=utf-8"  返回时估计按照ios编码了，如果以json格式也不会出现该问题，一般是按照
        return payHtml;
    }
}
